<template>
  <div class="users">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="users_div">
      <h3>用户列表</h3>
      <el-input placeholder="请输入内容" v-model="input" class="input-with-select">
        <el-button slot="append" @click="search()" icon="el-icon-search"></el-button>
      </el-input>
    </div>
    <div>
      <el-card shadow="always" class="cards">
        <el-button type="primary" @click="dialogVisible = true">添加</el-button>
      </el-card>
      <!--  -->
      <el-table
        ref="multipleTable"
        :data="userList.users"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        class="table"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="username" label="姓名" width="120"> </el-table-column>
        <el-table-column prop="mobile" label="手机号" width="200"> </el-table-column>
        <el-table-column prop="email" label="邮箱" width="250"> </el-table-column>
        <el-table-column prop="create_time" label="创建时间" width="200">
        </el-table-column>
        <el-table-column prop="role_name" label="角色名字" width="180"> </el-table-column>
        <el-table-column label="操作" width="270">
          <template slot-scope="scope">
            <el-button type="text" size="small">编辑</el-button>
            <el-button @click="del(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="50%"
        :before-close="handleClose"
      >
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              type="text"
              v-model="ruleForm.username"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input type="text" v-model="ruleForm.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="ruleForm.password"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input type="text" v-model="ruleForm.email" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { Message } from "element-ui";
export default {
  name: "users",
  data() {
    var validateemail = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入邮箱"));
      } else {
        callback();
      }
    };
    var validatephone = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入手机号"));
      } else {
        callback();
      }
    };
    var validatepassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入邮箱"));
      } else {
        callback();
      }
    };
    var validateusername = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入邮箱"));
      } else {
        callback();
      }
    };
    return {
      input: "",
      userList: {},
      dialogVisible: false,
      ruleForm: {
        username: "",
        phone: "",
        password: "",
        email: "",
      },
      rules: {
        username: [
          { validator: validateusername, message: "请输入用户名", trigger: "blur" },
        ],
        phone: [{ validator: validatephone, trigger: "blur" }],
        password: [
          { validator: validatepassword, message: "请输入密码", trigger: "blur" },
        ],
        email: [{ validator: validateemail, trigger: "blur" }],
      },
    };
  },
  methods: {
    search() {},
    handleSelectionChange() {},
    async getUserList() {
      var p1 = await this.axios.get("users?pagenum=1&pagesize=30");
      console.log(p1);
      this.userList = p1.data.data;
    },
    async del(a) {
      console.log(a.id);
      var p1 = await this.axios.delete("users/" + a.id);
      console.log(p1);
      if (p1.data.meta.status == 200) {
        Message({
          message: p1.data.meta.msg,
          center: true,
        });
        this.getUserList();
      } else {
        Message({
          message: p1.data.meta.msg,
          center: true,
        });
      }
    },
  },
  mounted() {
    this.getUserList();
  },
};
</script>
<style lang="scss">
.users {
  text-align: left;
  .el-card__body {
    padding: 0;
  }
  .cards {
    padding: 0 50px !important;
    height: 100px;
    line-height: 100px;
  }
}
.users_div {
  display: flex;
  justify-content: space-between;
  align-items: center;
  line-height: 55px;
  .input-with-select {
    width: 400px;
  }
}
</style>
